package fr.pud.client.controller.address;

import java.awt.event.ActionEvent;
import java.util.HashSet;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.Address;
import fr.pud.client.view.JMapViewerExtended;
import fr.pud.client.view.MainFrame;
import fr.pud.client.view.SearchFrame;
import fr.pud.utils.Conversion;

/**
 * The listener placed on the JTextField which enables to enter the place where
 * the map is supposed to be placed.
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         GotoListener.java
 */
public class GotoListener extends AbstractAction {
    /**
     * Serial UID
     */
    private static final long serialVersionUID = -7416803573294497232L;
    /**
     * The new zoom value is equal to CURRENT_VALUE + INCREASE_ZOOM_BY
     */
    private static final int  INCREASE_ZOOM_BY = 4;
    /**
     * A short link to the MainFrame singleton
     */
    private MainFrame         frame            = MainFrame.getMainFrame();

    /**
     * Default constructor sets the controller up.
     */
    public GotoListener() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getObject(LanguagesConstants.SEARCH));
    }

    /**
     * Opens the SearchFrame to allow the user to select the right place to go.
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        String searchText = this.frame.getGoToText().getText();
        if (!searchText.equals("")) {
            JMapViewerExtended.getJMapViewer().setZoom(
                    MainFrame.DEFAULT_ZOOM + INCREASE_ZOOM_BY);
            HashSet<Address> results = Conversion.geocoding(searchText);
            if (!results.contains(new Address())) {
                new SearchFrame(results);
            }
            this.frame.getGoToText().setText("");
        }
    }
}
